//----
// PART VI - FATCA and Dividends


	/* This file uses Schedule K-1 data with the jurisdiction
	of the recipient to describe patterns that occur around
	2010 and 2011. It also produces Figures 10 and 11. */

// ----------------
// Time Trends relevant for FATCA and Dividends
		
	// -----------------
	// FATCA
	
	// ------
	// Jump in Long-Term Capital Gains
	
		use ${data_dir}/income_table.dta, clear
				
		// exclude income flowing to higher-tier partnerships
		
		drop if class_entity == "P"		
		
		// drop US partners
		
		drop if destination=="US"
				
		// set income variable for analysis
		
		global inc = "box9a_netltcap"
		
		// Collapse
		
		gcollapse (sum) ${inc}, by(destination year)
		
		// Ogranize data for charts
		
			g y = 2005-1+_n 
				replace y = . if y>2019
			
			replace ${inc} = ${inc}/1e9
					
			local d_list "Cay Zer Hav Con For Und"
			tokenize "`d_list'"
			local nn: word count `d_list'
			forvalues xx = 1/`nn' {
				sum ${inc} if year==2005 & dest=="``xx''"
				g ``xx'' = r(mean) if y==2005
				}
				

			local d_list "Cay Zer Hav Con For Und"
			tokenize "`d_list'"
			local nn: word count `d_list'
			local t 2006
			forvalues xx = 1/`nn' {
				forvalues y = `t'/2019 {
					sum ${inc} if year==`y' & dest=="``xx''"
					replace ``xx'' = r(mean) if y==`y'
					}		
				}
			
		// Panel (a)

		// Clean
		
			keep y-Und
			keep if y<.
				
		// grayscale
		
		graph bar Cay Zer Hav Con For Und, ///
			over(y, gap(10) label(labsize(vsmall))) ///
			stack ///
			bar(1, color(gray*2)) ///
			bar(2, color(gray*1.3)) ///
			bar(3, color(gray*1)) ///
			bar(4, color(gray*.75)) ///
			bar(5, color(gray*.4)) ///
			bar(6, color(gray*.25)) ///
			plotregion(lcolor(black)) ///
			graphregion(fcolor(white) lcolor(white)) ///
			bgcolor(white) ///
			ytitle("Billions of 2020 Dollars", height(4)) ///
			ylabel(,) ///
			leg(pos(6) cols(3) order (1 "Cayman Islands" 2 "Zero Rate Havens" 3 "Other Havens" 4 "Conduit Countries" 5 "Other Countries" 6 "Undisclosed Foreign") region(lcolor(white)))		
			graph export ${fig_dir}/fig_fatca_nlt.png, replace width(1000)
		
		// color
		
		graph bar Cay Zer Hav Con For Und, ///
			over(y, gap(10) label(labsize(vsmall))) ///
			stack ///
			bar(1, color("133 41 40")) ///
			bar(2, color("237 144 90")) ///
			bar(3, color("232 202 90")) ///
			bar(4, color("121 168 108")) ///
			bar(5, color(navy*.8)) ///
			bar(6, color(gray*.8)) ///
			plotregion(lcolor(black)) ///
			graphregion(fcolor(white) lcolor(white)) ///
			bgcolor(white) ///
			ytitle("Billions of 2020 Dollars", height(4)) ///
			ylabel(,) ///
			leg(pos(6) cols(3) order (1 "Cayman Islands" 2 "Zero Rate Havens" 3 "Other Havens" 4 "Conduit Countries" 5 "Other Countries" 6 "Undisclosed Foreign") region(lcolor(white)))
			graph export ${fig_dir}/fig_fatca_nlt_color.png, replace width(1000)

			
		// Export
				
			foreach v of varlist Cay-Und {
				replace `v'= round(`v',.1)
				}
		
			export delimited using ${out_dir}/fig10a_tab.csv, replace
		
	// ------
	// US Owners, long-term capital gains
	
		use ${data_dir}/income_table.dta, clear
				
		// exclude income flowing to higher-tier partnerships
		
		drop if class_entity == "P"		
		
		// Narrow population
		
		keep if destination=="US"
		
		// Set relevant income
		
		global inc "box9a_netltcap"

		// Collapse
		
		gcollapse (sum) ${inc}, by(dest year)
		
		// Organize data for charts
		
		g y = 2005-1+_n 
			replace y = . if y>2019
		
		replace ${inc} = ${inc}/1e9
		
		g z = .
		
		// Panel (b)

		// Clean
		
			keep y ${inc} z
			keep if y<.
		
		// grayscale
		
		graph bar ${inc} z, ///
			over(y, gap(10) label(labsize(vsmall))) ///
			stack ///
			bar(1, color(gray)) ///
			bar(2, color(white)) ///
			plotregion(lcolor(black)) ///
			graphregion(fcolor(white) lcolor(white)) ///
			bgcolor(white) ///
			ytitle("Billions of 2020 Dollars", height(4)) ///
			ylabel(, ) ///
			leg(pos(6) cols(1) order(1 "Net Long-Term Capital Gains to US Owners" 2 " " ) region(lcolor(white)))							
			graph export ${fig_dir}/fig_fatca_nlt_us.png, replace width(1000)
			
		// color
		
		graph bar ${inc} z, ///
			over(y, gap(10) label(labsize(vsmall))) ///
			stack ///
			bar(1, color(navy*.8)) ///
			bar(2, color(white)) ///
			plotregion(lcolor(black)) ///
			graphregion(fcolor(white) lcolor(white)) ///
			bgcolor(white) ///
			ytitle("Billions of 2020 Dollars", height(4)) ///
			ylabel(, ) ///
			leg(pos(6) cols(1) order(1 "Net Long-Term Capital Gains to US Owners" 2 " " ) region(lcolor(white)))							
			graph export ${fig_dir}/fig_fatca_nlt_us_color.png, replace width(1000)
			
		
		// Export
		
			replace ${inc}= round(${inc},0.1)
				
			export delimited using ${out_dir}/fig10b_tab.csv, replace
		
		
	// ------
	// Ordinary Payments to Custodial and Trust Non-Filers
	
		use ${data_dir}/income_table.dta, clear
				
		// exclude income flowing to higher-tier partnerships
		
		drop if class_entity == "P"		
		
		// set income variable for analysis
		
		g relevant_income = box1_ordinary+box2_netrental+box3_othernetrental+box5_interest+box7_royalties
		global inc "relevant_income"
		
		// drop US partners
		
		drop if destination=="US"
				
		// drop partners matched to annual return
		
		keep if matched_to_filing==0
		
		// keep trusts and custodial accounts
		
		keep if inlist(class_entity,"T","D")
		
		// Collapse
		
		gcollapse (sum) ${inc}, by(destination year)
		
		// Organize for Charts
		
			g y = 2005-1+_n 
				replace y = . if y>2019
			
			replace ${inc} = ${inc}/1e9
					
			local d_list "Cay Zer Hav Con For Und"
			tokenize "`d_list'"
			local nn: word count `d_list'
			forvalues xx = 1/`nn' {
				sum ${inc} if year==2005 & dest=="``xx''"
				g ``xx'' = r(mean) if y==2005
				}
				

			local d_list "Cay Zer Hav Con For Und"
			tokenize "`d_list'"
			local nn: word count `d_list'
			local t 2006
			forvalues xx = 1/`nn' {
				forvalues y = `t'/2019 {
					sum ${inc} if year==`y' & dest=="``xx''"
					replace ``xx'' = r(mean) if y==`y'
					}		
				}
		
		// Panel (c)

		// Clean
		
			keep y-Und
			keep if y<.
		
		
		// grayscale
		
		graph bar Cay Zer Hav Con For, ///
			over(y, gap(10) label(labsize(vsmall))) ///
			stack ///
			bar(1, color(gray*2)) ///
			bar(2, color(gray*1.3)) ///
			bar(3, color(gray*1)) ///
			bar(4, color(gray*.75)) ///
			bar(5, color(gray*.4)) ///
			plotregion(lcolor(black)) ///
			graphregion(fcolor(white) lcolor(white)) ///
			bgcolor(white) ///
			ytitle("Billions of 2020 Dollars", height(4)) ///
			ylabel(, ) ///
			leg(pos(6) cols(3) order (1 "Cayman Islands" 2 "Zero Rate Havens" 3 "Other Havens" 4 "Conduit Countries" 5 "Other Countries" ) region(lcolor(white)))
			graph export ${fig_dir}/fig_fatca_ord_cust.png, replace width(1000)
			
		// color
		
		graph bar Cay Zer Hav Con For, ///
			over(y, gap(10) label(labsize(vsmall))) ///
			stack ///
			bar(1, color("133 41 40")) ///
			bar(2, color("237 144 90")) ///
			bar(3, color("232 202 90")) ///
			bar(4, color("121 168 108")) ///
			bar(5, color(navy*.8)) ///
			plotregion(lcolor(black)) ///
			graphregion(fcolor(white) lcolor(white)) ///
			bgcolor(white) ///
			ytitle("Billions of 2020 Dollars", height(4)) ///
			ylabel(, ) ///
			leg(pos(6) cols(3) order (1 "Cayman Islands" 2 "Zero Rate Havens" 3 "Other Havens" 4 "Conduit Countries" 5 "Other Countries" ) region(lcolor(white)))
			graph export ${fig_dir}/fig_fatca_ord_cust_color.png, replace width(1000)
			
			
		// Export
		
			foreach v of varlist Cay-Und {
				replace `v'= round(`v',.1)
				}
		
			export delimited using ${out_dir}/fig10c_tab.csv, replace
		

			
			
	// Ordinary Payments to Filers
	
		use ${data_dir}/income_table.dta, clear
				
		// exclude income flowing to higher-tier partnerships
		
		drop if class_entity == "P"		
		
		// set income variable for analysis
				
		g relevant_income = box1_ordinary+box2_netrental+box3_othernetrental+box5_interest+box7_royalties
		global inc "relevant_income"

		// keep only partners matched to annual returns
		
		keep if matched_to_filing==1
		
		// drop US partners
		
		drop if destination=="US"
				
		// Collapse
		
		gcollapse (sum) ${inc}, by(destination year)
		
			// Organize for Charts
		
			g y = 2005-1+_n 
				replace y = . if y>2019
			
			replace ${inc} = ${inc}/1e9
					
			local d_list "Cay Zer Hav Con For Und"
			tokenize "`d_list'"
			local nn: word count `d_list'
			forvalues xx = 1/`nn' {
				sum ${inc} if year==2005 & dest=="``xx''"
				g ``xx'' = r(mean) if y==2005
				}
				

			local d_list "Cay Zer Hav Con For Und"
			tokenize "`d_list'"
			local nn: word count `d_list'
			local t 2006
			forvalues xx = 1/`nn' {
				forvalues y = `t'/2019 {
					sum ${inc} if year==`y' & dest=="``xx''"
					replace ``xx'' = r(mean) if y==`y'
					}		
				}
		
		// Panel (d)

		// Clean
		
			keep y-Und 
			keep if y<.
					
		
		// grayscale
		
		graph bar Cay Zer Hav Con For Und, ///
			over(y, gap(10) label(labsize(vsmall))) ///
			stack ///
			bar(1, color(gray*2)) ///
			bar(2, color(gray*1.3)) ///
			bar(3, color(gray*1)) ///
			bar(4, color(gray*.75)) ///
			bar(5, color(gray*.4)) ///
			bar(6, color(gray*.25)) ///
			plotregion(lcolor(black)) ///
			graphregion(fcolor(white) lcolor(white)) ///
			bgcolor(white) ///
			ytitle("Billions of 2020 Dollars",  height(4)) ///
			ylabel(,) ///
			leg(pos(6) cols(3) order (1 "Cayman Islands" 2 "Zero Rate Havens" 3 "Other Havens" 4 "Conduit Countries" 5 "Other Countries" 6 "Undisclosed Foreign") region(lcolor(white)))
			graph export ${fig_dir}/fig_fatca_ord_file.png, replace width(1000)
		
		// color
		
		graph bar Cay Zer Hav Con For Und, ///
			over(y, gap(10) label(labsize(vsmall))) ///
			stack ///
			bar(1, color("133 41 40")) ///
			bar(2, color("237 144 90")) ///
			bar(3, color("232 202 90")) ///
			bar(4, color("121 168 108")) ///
			bar(5, color(navy*.8)) ///
			bar(6, color(gray*.8)) ///
			plotregion(lcolor(black)) ///
			graphregion(fcolor(white) lcolor(white)) ///
			bgcolor(white) ///
			ytitle("Billions of 2020 Dollars",  height(4)) ///
			ylabel(,) ///
			leg(pos(6) cols(3) order (1 "Cayman Islands" 2 "Zero Rate Havens" 3 "Other Havens" 4 "Conduit Countries" 5 "Other Countries" 6 "Undisclosed Foreign") region(lcolor(white)))
			graph export ${fig_dir}/fig_fatca_ord_file_color.png, replace width(1000)
			
		// Export
		
			foreach v of varlist Cay-Und {
				replace `v'= round(`v',.1)
				}
		
			export delimited using ${out_dir}/fig10d_tab.csv, replace
		


	// -----------------
	// Dividends
	
	// ------
	// High-Tax Jurisdiction Countries
	
		use ${data_dir}/income_table.dta, clear
				
		// exclude income flowing to higher-tier partnerships
		
		drop if class_entity == "P"		
		
		// set income variable for analysis
		
		global inc = "box6a_dividends"
		
		// drop low-tax treaty countries
		
		keep if treaty_low_div==0
		
		// collapse
		
		gcollapse (sum) ${inc}, by(dest year)
		
		// organize the data
		
			g y = 2005-1+_n 
				replace y = . if y>2019
			
			replace ${inc} = ${inc}/1000000000
					
			local d_list "Cay Zer Hav Con For Und"
			tokenize "`d_list'"
			local nn: word count `d_list'
			forvalues xx = 1/`nn' {
				sum ${inc} if year==2005 & dest=="``xx''"
				g ``xx'' = r(mean) if y==2005
				}
				

			local d_list "Cay Zer Hav Con For Und"
			tokenize "`d_list'"
			local nn: word count `d_list'
			local t 2006
			forvalues xx = 1/`nn' {
				forvalues y = `t'/2019 {
					sum ${inc} if year==`y' & dest=="``xx''"
					replace ``xx'' = r(mean) if y==`y'
					}		
				}
					
		// Panel (a)

		keep y Cay-Und
	
		// grayscale
		
		graph bar Cay Zer Hav Con For Und, ///
			over(y, gap(10) label(labsize(vsmall))) ///
			stack ///
			bar(1, color(gray*2)) ///
			bar(2, color(gray*1.3)) ///
			bar(3, color(gray*1)) ///
			bar(4, color(gray*.75)) ///
			bar(5, color(gray*.4)) ///
			bar(6, color(gray*.25)) ///
			plotregion(lcolor(black)) ///
			graphregion(fcolor(white) lcolor(white)) ///
			bgcolor(white) ///
			ytitle("Billions of 2020 Dollars", height(4)) ///
			ylabel(,) ///
			leg(pos(6) cols(3) order (1 "Cayman Islands" 2 "Zero Rate Havens" 3 "Other Havens" 4 "Conduit Countries" 5 "Other Countries" 6 "Undisclosed Foreign") region(lcolor(white)))
			graph export ${fig_dir}/fig_div_notreaty.png, replace width(1000)
			
		
		// color
		
		graph bar Cay Zer Hav Con For Und, ///
			over(y, gap(10) label(labsize(vsmall))) ///
			stack ///
			bar(1, color("133 41 40")) ///
			bar(2, color("237 144 90")) ///
			bar(3, color("232 202 90")) ///
			bar(4, color("121 168 108")) ///
			bar(5, color(navy*.8)) ///
			bar(6, color(gray*.8)) ///
			plotregion(lcolor(black)) ///
			graphregion(fcolor(white) lcolor(white)) ///
			bgcolor(white) ///
			ytitle("Billions of 2020 Dollars", height(4)) ///
			ylabel(,) ///
			leg(pos(6) cols(3) order (1 "Cayman Islands" 2 "Zero Rate Havens" 3 "Other Havens" 4 "Conduit Countries" 5 "Other Countries" 6 "Undisclosed Foreign") region(lcolor(white)))
			graph export ${fig_dir}/fig_div_notreaty_color.png, replace width(1000)
						
		// Export
		
			foreach v of varlist Cay-Und {
				replace `v'= round(`v',.1)
				}
		
			export delimited using ${out_dir}/fig11a_tab.csv, replace

	// ------
	// Low-Tax Treaty Countries
	
		use ${data_dir}/income_table.dta, clear
				
		// exclude income flowing to higher-tier partnerships
		
		drop if class_entity == "P"		
		
		// set income variable for analysis
		
		global inc = "box6a_dividends"
		
		// keep only low-tax treaty countries
		
		keep if treaty_low_div==1
		
		// collapse
		
		gcollapse (sum) ${inc}, by(dest year)
		
		// organize the data
		
			g y = 2005-1+_n 
				replace y = . if y>2019
			
			replace ${inc} = ${inc}/1000000000
					
			local d_list "Cay Zer Hav Con For Und"
			tokenize "`d_list'"
			local nn: word count `d_list'
			forvalues xx = 1/`nn' {
				sum ${inc} if year==2005 & dest=="``xx''"
				g ``xx'' = r(mean) if y==2005
				}
				

			local d_list "Cay Zer Hav Con For Und"
			tokenize "`d_list'"
			local nn: word count `d_list'
			local t 2006
			forvalues xx = 1/`nn' {
				forvalues y = `t'/2019 {
					sum ${inc} if year==`y' & dest=="``xx''"
					replace ``xx'' = r(mean) if y==`y'
					}		
				}
				
				
			keep y Hav Con For

		// Panel (b)

		// grayscale
		
		graph bar Hav Con For, ///
			over(y, gap(10) label(labsize(vsmall))) ///
			stack ///
			bar(1, color(gray*2)) ///
			bar(2, color(gray*.9)) ///
			bar(3, color(gray*.4)) ///
			plotregion(lcolor(black)) ///
			graphregion(fcolor(white) lcolor(white)) ///
			bgcolor(white) ///
			ytitle("Billions of 2020 Dollars", height(4)) ///
			ylabel(, ) ///
			leg(pos(6) cols(3) order (1 "Other Havens" 2 "Conduit Countries" 3 "Other Countries" ) region(lcolor(white)))
			graph export ${fig_dir}/fig_div_treaty.png, replace width(1000)
			
		// color
		
		graph bar Hav Con For, ///
			over(y, gap(10) label(labsize(vsmall))) ///
			stack ///
			bar(1, color("232 202 90")) ///
			bar(2, color("121 168 108")) ///
			bar(3, color(navy*.8)) ///
			plotregion(lcolor(black)) ///
			graphregion(fcolor(white) lcolor(white)) ///
			bgcolor(white) ///
			ytitle("Billions of 2020 Dollars", height(4)) ///
			ylabel(, ) ///
			leg(pos(6) cols(3) order (1 "Other Havens" 2 "Conduit Countries" 3 "Other Countries" ) region(lcolor(white)))
			graph export ${fig_dir}/fig_div_treaty_color.png, replace width(1000)
	
	
		// Export
		
			foreach v of varlist Hav-For {
				replace `v'= round(`v',.1)
				}
		
			export delimited using ${out_dir}/fig11b_tab.csv, replace
		

	
		
